using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;



namespace PostXING.Controls
{
    /// <summary>
    /// GradientPanel is just like a regular panel except it optionally  
    /// shows a gradient.
    /// </summary>
    [ToolboxBitmap(typeof(Panel))]
    public class GradientPanel : Panel
    {
        /// <summary>
        /// Property GradientColor (Color)
        /// </summary>
        private Color _gradientColor;
        public Color GradientColor
        {
            get { return this._gradientColor; }
            set { this._gradientColor = value; }
        }

        /// <summary>
        /// Property Raised (bool)
        /// </summary>
        private bool _raised;
        public bool Raised
        {
            get { return _raised; }
            set { _raised = value; }
        }

        /// <summary>
        /// Property GlassColor (Color)
        /// </summary>
        private Color _glassColor;
        public Color GlassColor
        {
            get { return _glassColor; }
            set { _glassColor = value; }
        }

        /// <summary>
        /// Property GlassyLook (bool)
        /// </summary>
        private bool _glassyLook;
         public bool GlassyLook
        {
          get { return _glassyLook; }
          set { _glassyLook = value; }
        }

        /// <summary>
        /// Property Rotation (float)
        /// </summary>
        private float _rotation;
        public float Rotation
        {
            get { return this._rotation; }
            set { this._rotation = value; }
        }

        /// <summary>
        /// Property Beveled (bool)
        /// </summary>
        private bool _beveled;
        public bool Beveled
        {
            get { return _beveled; }
            set { _beveled = value; }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (e.ClipRectangle.IsEmpty) return; //why draw if non-visible?
            if (!_glassyLook)
            {
                

                using (LinearGradientBrush lgb = new
                               LinearGradientBrush(this.ClientRectangle,
                          this.BackColor,
                          this.GradientColor,
                          this.Rotation))
                {
                    e.Graphics.FillRectangle(lgb, this.ClientRectangle);
                }
            }
            else
            {
                Rectangle topRectangle =
                    new Rectangle(this.ClientRectangle.X, this.ClientRectangle.Y,
                    this.ClientRectangle.Width, (this.ClientRectangle.Height / 2));
                Rectangle bottomRectangle =
                    new Rectangle(this.ClientRectangle.X, (this.ClientRectangle.Height / 2),
                    this.ClientRectangle.Width, this.ClientRectangle.Height -
                    this.ClientRectangle.Height / 2);
                using (LinearGradientBrush lgb = new
                               LinearGradientBrush(this.ClientRectangle,
                          this.GlassColor,
                          this.GradientColor,
                          90))
                {
                    e.Graphics.FillRectangle(lgb, topRectangle);
                } 
            }

            if (_raised)
            {
                Graphics g = e.Graphics;

                Rectangle panelRect = this.ClientRectangle;

                Point p1 = new Point(panelRect.Left, panelRect.Top); 
                Point p2 = new Point(panelRect.Right - 1, panelRect.Top); 
                Point p3 = new Point(panelRect.Left, panelRect.Bottom - 1); 
                Point p4 = new Point(panelRect.Right - 1, panelRect.Bottom - 1); 

                Point p5 = new Point(panelRect.Left+1, panelRect.Top+1); 
                Point p6 = new Point(panelRect.Right - 2, panelRect.Top+1); 
                Point p7 = new Point(panelRect.Left+1, panelRect.Bottom - 2); 
                Point p8 = new Point(panelRect.Right - 2, panelRect.Bottom - 2); 


                if (_beveled)
                {
                    Pen pen1 = new Pen(System.Drawing.Color.FromArgb(167,166,170));
                    Pen pen2 = new Pen(System.Drawing.Color.White);
                    g.DrawLine(pen2, p1, p2);
                    g.DrawLine(pen2, p1, p3);
                    g.DrawLine(pen2, p2, p4);
                    g.DrawLine(pen2, p3, p4);
                    g.DrawLine(pen1, p5, p6);
                    g.DrawLine(pen1, p5, p7);
                    g.DrawLine(pen1, p6, p8);
                    g.DrawLine(pen1, p7, p8);
                }
                else
                {
                    Pen pen1 = new Pen(System.Drawing.Color.White);
                    Pen pen2 = new Pen(System.Drawing.Color.Gray);
                    g.DrawLine(pen1, p1, p2);
                    g.DrawLine(pen1, p1, p3);
                    g.DrawLine(pen2, p2, p4);
                    g.DrawLine(pen2, p3, p4);
                }
            }

            base.OnPaint(e); //right, want anything handled to be drawn too.
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ResumeLayout(false);

        }
    }
}
